RedDemandante = function(){
	
	var index = 0;
	return {
		safeForm : function(){
			if(index==0)
				RedDemandante.validateDocument();
			if(index==1)
				RedDemandante.saveDemandante();
				
		},
		validateDocument: function(){
			Asg.Message.progress("Validando Informaci\u00F3n...");
			Ext.Ajax.request({
				timeout: Asg.constants.TIMEOUT_SECONDS,
				url: Asg.constants.CONTEXT_PATH + "/admin/demandante/validateDocument",
				params:{
					ctgDocumentoId:  Asg.Utils.getValue("ctgDocumentoId"),
					redDemandanteNumeroDocumento: Asg.Utils.getValue("redDemandanteNumeroDocumento")
				},
				callback: function(options, success, response){
					
					if(success){
						var jsonObject = Asg.Utils.ajaxRequestGetJson(response);
						if(!success || !jsonObject){
							Asg.Message.alertInvalid(Asg.constants.DEFAULT_ERROR_MESSAGE);
							return
						}
						if(jsonObject.success){
							if(jsonObject.validate){
								Asg.Message.alert(Asg.constants.VALIDATE_DOCUMENT);
							}else{
								Ext.Msg.hide();
								RedDemandante.next();
							}
							
						}
					}
				}
			});
		},
		saveDemandante: function(){
			Asg.Message.progress("Guardando Informaci\u00F3n...");
			
			
			var form = Ext.getCmp("formDatosPersonalesDemandante").getForm();
			if(!form.isValid()){
				Asg.Message.alertInvalidFields();
				return;
			}
			Asg.Message.progress("Guardando Informacion...");
			form.submit({
				url: Asg.constants.CONTEXT_PATH + "/admin/demandante/save",
				timeout: Asg.constants.TIMEOUT_SECONDS,
				params:{
					"redDemandante.ctgDocumento.ctgDocumentoId":Ext.getCmp("ctgDocumentoId").getValue(),
					"redDemandanteNumeroDocumento":Ext.getCmp("redDemandanteNumeroDocumento").getValue()
				},
			    success: function(form, action) {
			    	Asg.Grid.loadData("gridDemandantes", action.result.demandante);
			    	Ext.Msg.hide();
			    	RedDemandante.next();
					//Ext.getCmp("registroWindow").hide();
			    },
			    failure: Asg.Form.failureProcedure
			});
		},
		 
		next: function(){
			var form = Ext.getCmp("asistenteDemandante"); 
			Ext.getCmp("registroWindow").maximize();
			Ext.getCmp("btnBack").show();
			if(index < form.items.length-1){
				index++;
				var cardlayout = form.getLayout();
				cardlayout.setActiveItem(index);
				if(index == form.items.length-1){
					Ext.getCmp("btnValidate").hide();
					Ext.getCmp("btnFinish").show();
				}
			}
		},
		back: function(){
			var form = Ext.getCmp("asistenteDemandante"); 
			if(index > 0){
				index --;
				var cardlayout = form.getLayout();
				cardlayout.setActiveItem(index);
			}
			
			if(index == 0){
				Ext.getCmp("btnBack").hide();
				Ext.getCmp("btnValidate").show();
				Ext.getCmp("btnFinish").hide();
			}else{
				Ext.getCmp("btnValidate").show();
				Ext.getCmp("btnFinish").hide();
			}
		},
		finish: function(){
			
		},
		registroWindow: function(ctgMunicipios,ctgOficina,sgdUsuario,ctgDocumento,ctgGenero,ctgEstadoCivil,
				ctgTipoLicencia,ctgDocumentoVigentes,ctgTipoVehiculo,ctgEscolaridad,ctgEspecialidad,ctgIdioma,ctgNivelIdioma){
			index = 0;
			new Ext.Window({
				id: "registroWindow",
				layout: "fit", 
				title: Asg.constants.APPLICATION_TITLE + " .::. ASISTENTE DE DEMANDANTE",
				modal: true,
				width: 600,
				maximizable: true,
				resizable	: true,
	        	bodyStyle: "background: #DFE8F6",
	        	items:  new Ext.FormPanel({
	    			layout: "card",
	    			border: false,
	    			id: "asistenteDemandante",
	    			frame: true,
	    			activeItem: RedDemandante.index,
	    			items: [
	    			    RedDemandantesForms.seccionControlDemandante(ctgOficina,sgdUsuario,ctgDocumento),
	    			    RedDemandantesForms.datosPersonalesDemandante(ctgMunicipios,ctgGenero,ctgEstadoCivil,ctgTipoLicencia,ctgDocumentoVigentes,ctgTipoVehiculo),
	    			    RedDemandantesForms.formacionAcademicaDemandante(ctgEscolaridad,ctgEspecialidad,ctgIdioma,ctgNivelIdioma)
	    			    		
	     		    ]
	    		}),
	    		fbar: [
	    		       new Ext.Button({
	    		    	   text : "Validar Informaci\u00F3n",
	    		    	   id: "btnValidate",
	    		    	   name: "btnValidate",
	    		    	   handler : function(){RedDemandante.safeForm();},
	    		    	   scope : this
    		    	   }),
    		    	   new Ext.Button({
    		    		   text: "Atras",
    		    		   id: "btnBack",
    		    		   name: "brnBack",
    		    		   hidden: true,
    		    		   handler: function(){RedDemandante.back();},
    		    		   scope: this
    		    	   }),
    		    	   new Ext.Button({
    		    		   text: "Finalizar",
    		    		   id: "btnFinish",
    		    		   name: "btnFinish",
    		    		   hidden: true,
    		    		   handler: function(){RedDemandante.safeForm();},
    		    		   scope: this
    		    	   })
		        ]
			}).show();
		},
		initConfig: function(config){
			config = config || {};
			Ext.define("redDemandanteModel",{
				extend: "Ext.data.Model",
				fields:[
			        "redDemandanteFolio","redDemandanteFullName","redDemandanteFechaRegistro",
			        "wkfEstadoSeguimiento","sgdUsuario","redDemandanteDUI","ctgOficina"
		        ]
			});
			return {
				items: {
					xtype: 'grid',
					id: 'gridDemandantes',
					flex: 1,
					margins: "5 5 5 5",
					store: new Ext.data.Store({
						data: config.data || [],
						model: "redDemandanteModel",
				        proxy: {
				            type: "memory",
				            reader: {
				                type: "array"
				            }
				        }
					}),
					title: 'CONSULTA DE DEMANDANTES',
					columns: [
				          {
				        	xtype:"actioncolumn",
				        	width: 150,
		        			items: [
	        			        {
	        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/edit.png",
	        			        	tooltip: "EDITAR DEAMANDANTE",
	        			        	iconCls: "action_icons",
	        			        	handler: function(grid, rowIndex, colIndex){
	        			        		var record = grid.getStore().getAt(rowIndex);
	        			        		if(record != null){
//	        			        			RedNacionalEmpleo.setRedDemandanteFolio(record.get("redDemandanteFolio"));
//	        			        			RedDemandante.registroWindow(
//	 	     	        	    				   config.ctgMunicipios || [], config.ctgOficina || [],
//	 	     	        	    				   config.sgdUsuario || [], config.ctgDocumento || [], 
//	 	     	        	    				   config.ctgGenero, config.ctgEstadoCivil || [],
//	 	     	        	    				   config.ctgTipoLicencia || [], config.ctgDocumentoVigentes || [], 
//	 	     	        	    				   config.ctgTipoVehiculo || [], config.ctgEscolaridad || [],
//	 	     	        	    				   config.ctgEspecialidad || [], config.ctgIdioma ||[],
//	 	     	        	    				   config.ctgNivelIdioma || []);
	        			        		}
	        			        		
	        			        		
	        			        	}
	        			        }, {
	        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/page_pdf.png",
	        			        	tooltip: "VER FICHA",
	        			        	iconCls: "action_icons"
	        			        },{
	        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/find.png",
	        			        	tooltip: "BUSCAR OFERTA",
	        			        	iconCls: "action_icons"
	        			        },{
	        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/user_go.png",
	        			        	tooltip: "AFECTAR DEMANDANTE",
	        			        	iconCls: "action_icons"
	        			        },{
	        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/verification.png",
	        			        	tooltip: "ACTIVAR / INACTIVAR",
	        			        	iconCls: "action_icons",
	        			        	handler: function(){
	        			        		RedActivacionDemandante.activacionDemandante();
	        			        	}
	        			        },{
	        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/comments.png",
	        			        	tooltip: "OBSERVACIONES",
	        			        	iconCls: "action_icons",
	        			        	handler: function(grid, rowIndex, colIndex){
	        			        		var record = grid.getStore().getAt(rowIndex);
	        			        		if(record != null){
	        			        			RedObservaciones.bitacoraObservaciones(record.get("redDemandanteFullName"),record.get("redDemandanteFolio"));
	        			        		}
	        			        		
	        			        	}
	        			        },{
	        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/cancel.png",
	        			        	tooltip: "ELIMINAR DEMANDANTE",
	        			        	iconCls: "action_icons"
	        			        }
	    			        ]
				          },
				          {header: 'Nombres', dataIndex: 'redDemandanteFullName',flex:1},
					],
					plugins: [{
						 ptype: 'rowexpander',
						 rowBodyTpl : [
						    "<center><table width='250px'>" +
						    "	<tr>" +
						    "		<td><b>ESTADO SEGUIMIENTO</b></td>" +
						    "		<td><b>FECHA REGISTRO</b></td>" +
						    "	</tr>" +
						    "	<tr>" +
						    "		<td>{wkfEstadoSeguimiento}</td>" +
						    "		<td>{redDemandanteFechaRegistro}</td>" +
						    "	</tr>" +
						    "	<tr>" +
						    "		<td><b>USUARIO</b></td>" +
						    "		<td><b>OFICINA</b></td>" +
						    "	</tr>" +
						    "	<tr>" +
						    "		<td>{sgdUsuario}</td>" +
						    "		<td>{ctgOficina}</td>" +
						    "	</tr>" +
						    "</table></center>"
			             ]
					}],
					tbar: [
	        	       "->",
	        	       {
	        	    	   text: "Agregar Demandante", 
	        	    	   iconCls: Asg.constants.ADD_ICON,
	        	    	   handler: function(){
	        	    		   RedDemandante.registroWindow(
	        	    				   config.ctgMunicipios || [], config.ctgOficina || [],
	        	    				   config.sgdUsuario || [], config.ctgDocumento || [], 
	        	    				   config.ctgGenero, config.ctgEstadoCivil || [],
	        	    				   config.ctgTipoLicencia || [], config.ctgDocumentoVigentes || [], 
	        	    				   config.ctgTipoVehiculo || [], config.ctgEscolaridad || [],
	        	    				   config.ctgEspecialidad || [], config.ctgIdioma ||[],
	        	    				   config.ctgNivelIdioma || []);
	        	    	   }
        	    	   }
        	        ]
				}
			};
		}
	};
}();